from tkinter import *


class LoanCalculator:
	def __init__(self):
		window = Tk()
		window.title("LoanCalculator")

		Label(window, text = "Annual Interest Rate").grid(row = 1,column = 1)
		Label(window, text = "Number Of Years").grid(row = 2,column = 1)
		Label(window, text = "Loan Amount").grid(row = 3,column = 1)
		Label(window, text = "Monthly Payment").grid(row = 4,column = 1)
		Label(window, text = "Total Payment").grid(row = 5,column = 1)

		self.rate = StringVar()
		Entry(window, textvariable = self.rate).grid(row = 1, column = 2)
		self.year = StringVar()
		Entry(window, textvariable = self.year).grid(row = 2, column = 2)
		self.lAmount = StringVar()
		Entry(window, textvariable = self.lAmount).grid(row = 3, column = 2)

		self.MPaymentVar = StringVar()
		MPayment = Label(window,text = "None", textvariable = self.MPaymentVar).grid(row = 4,column = 2,sticky = E)
		self.TPaymentVar = StringVar()
		TPayment = Label(window,text = "None", textvariable = self.TPaymentVar).grid(row = 5,column = 2,sticky = E)

		Button(window, text = "Compute Payment", command = self.processComputerPayment).grid(row = 6,column=2, sticky = E)

		window.mainloop()

	def processMPayment(self,rate,year,lAmount):
		Mpayment = lAmount * rate/(1-1/((1+rate)**(year*12)))
		return Mpayment

	def processComputerPayment(self):
		MPayment = self.processMPayment(float(self.rate.get())/1200, float(self.year.get()), int( self.lAmount.get() ) )
		self.MPaymentVar.set(format(MPayment, "10.2f"))
		'''TPament = float(self.MPaymentVar.get()) * 12  * int(self.year.get())'''
		TPament = self.processTPayment(float(self.year.get()))
		self.TPaymentVar.set(format(TPament, "10.2f"))

	def processTPayment(self,year):
		Mpayment = self.processMPayment(float(self.rate.get())/1200, float(self.year.get()), int( self.lAmount.get() ))
		NOYears = int(self.year.get())
		TPayment = Mpayment * NOYears * 12
		return TPayment


LoanCalculator()